package com.ustarlink.activityanimations;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by cfm on 16-4-8.
 */
public class BitmapUtils {
    int[] mPhotos = {R.drawable.p1,
            R.drawable.p2,
            R.drawable.p3,
            R.drawable.p4,
    };

    String[] mDescriptions = {
            "This picture was taken while sunbathing in a natural hot spring, which was " +
                    "unfortunately filled with acid, which is a lasting memory from that trip, whenever I " +
                    "I look at my own skin.",

            "I took this shot with a pinhole camera mounted on a tripod constructed out of " +
                    "soda straws. I felt that that combination best captured the beauty of the landscape " +
                    "in juxtaposition with the detritus of mankind.",

            "I don't remember where or when I took this picture. All I know is that I was really " +
                    "drunk at the time, and I woke up without my left sock.",

            "Right before I took this picture, there was a busload of school children right " +
                    "in my way. I knew the perfect shot was coming, so I quickly yelled 'Free candy!!!' " +
                    "and they scattered."
    };

    static HashMap<Integer, Bitmap> sBitmapResourceMap = new HashMap<Integer, Bitmap>();

    /**
     * Load pictures and descriptions, A real app wouldn't do it this way, but that's
     * not the point of this animation demo, Loading asynchronously is a better way to go
     * for what can be time-consuming operations
     */
    public ArrayList<PictureData> loadPhotos(Resources resources){
        ArrayList<PictureData> pictures = new ArrayList<PictureData>();
        for (int i = 0; i < 30; ++i){
            int resourceId = mPhotos[(int) (Math.random() * mPhotos.length)];
            Bitmap bitmap = getBitmap(resources, resourceId);
            Bitmap thumnail = getThumnail(bitmap, 200);
            String description = mDescriptions[(int) (Math.random() * mDescriptions.length)];
            pictures.add(new PictureData(resourceId, description, thumnail));
        }
        return pictures;
    }

    /**
     * Create and return a thumbnail image given the original source bitmap a max
     * dimension (with or height)
     * @param original
     * @param maxDimension
     * @return
     */
    private static Bitmap getThumnail(Bitmap original, int maxDimension) {
        int width = original.getWidth();
        int height = original.getHeight();
        int scaleWidth = 0, scaleHeight = 0;
        if (width >= height){
            float scaleFactor = (float) maxDimension / width;
            scaleWidth = 200;
            scaleHeight = (int) (scaleFactor * width);
        } else {
            float scaleFactor = (float) maxDimension / height;
            scaleWidth = (int) (scaleFactor * width);
            scaleHeight = 200;
        }
        Bitmap thumbnail = Bitmap.createScaledBitmap(original, scaleWidth, scaleHeight, true);
        return thumbnail;
    }

    /**
     * Utility method t oget bitmap from cache or, if not there, load it
     * from its resource
     * @param resources
     * @param resourceId
     * @return
     */
    static Bitmap getBitmap(Resources resources, int resourceId) {
        Bitmap bitmap = sBitmapResourceMap.get(resourceId);
        if (bitmap == null){
            bitmap = BitmapFactory.decodeResource(resources, resourceId);
            sBitmapResourceMap.put(resourceId, bitmap);
        }
        return bitmap;
    }
}
